package com.atguigu.crowd.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.atguigu.crowd.entity.po.AdressPO;
import com.atguigu.crowd.entity.po.AdressPOExample;
import com.atguigu.crowd.entity.po.OrderPO;
import com.atguigu.crowd.entity.po.OrderProjectPO;
import com.atguigu.crowd.entity.vo.AddressVO;
import com.atguigu.crowd.entity.vo.OrderProjectVO;
import com.atguigu.crowd.entity.vo.OrderVO;
import com.atguigu.crowd.mapper.AdressPOMapper;
import com.atguigu.crowd.mapper.OrderPOMapper;
import com.atguigu.crowd.mapper.OrderProjectPOMapper;
import com.atguigu.crowd.service.api.OrderService;


@Service
@Transactional(readOnly = true)
public class OrderServiceImpl implements OrderService {
	 @Autowired
	    private OrderProjectPOMapper orderProjectPOMapper;
	    @Autowired
	    private OrderPOMapper orderPOMapper;
	    @Autowired
	    private AdressPOMapper addressPOMapper;

	    @Override
	    public OrderProjectVO getOrderProjectVO( Integer returnId) {
	        return orderProjectPOMapper.selectOrderProjectVO(returnId);
	    }

	    @Override
	    public List<AddressVO> getAddressVOList(Integer memberId) {

	        AdressPOExample addressPOExample = new AdressPOExample();
	        addressPOExample.createCriteria().andMemberIdEqualTo(memberId);
	        List<AdressPO> addressPOList = addressPOMapper.selectByExample(addressPOExample);;
	        List<AddressVO> addressVOList = new ArrayList<>();
	        for (AdressPO addressPO : addressPOList) {
	            AddressVO addressVO = new AddressVO();
	            BeanUtils.copyProperties(addressPO,addressVO);
	            addressVOList.add(addressVO);
	        }


	        return addressVOList;
	    }

	    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
	    @Override
	    public void saveAddress(AddressVO addressVO) {

	        AdressPO addressPO = new AdressPO();

	        BeanUtils.copyProperties(addressVO,addressPO);

	        addressPOMapper.insert(addressPO);
	    }

	    // 保存支付宝订单信息
	    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
	    @Override
	    public void saveOrder(OrderVO orderVO) {
	        OrderPO orderPO = new OrderPO();
	        BeanUtils.copyProperties(orderVO,orderPO);
	        OrderProjectPO orderProjectPO = new OrderProjectPO();
	        BeanUtils.copyProperties(orderVO.getOrderProjectVO(),orderProjectPO);
	        // 保存orderPO并生成orderProjectPO的OrderId外键
	        orderPOMapper.insert(orderPO);
	        // 获取id  在OrderPOMapper.xml进行设置
	        // <insert id="insert" parameterType="com.example.entity.po.OrderPO"
	        // useGeneratedKeys="true" keyProperty="id">

	        Integer id = orderPO.getId();
	        // 作为orderProjectPO外键
	        orderProjectPO.setOrderId(id);

	        orderProjectPOMapper.insert(orderProjectPO);
	    }
}
